<?php
	session_start();
	require_once('../../../includes/constantes.inc.php');
	if(!require_once('../../../'.DIR_INCLUDE . '/function.inc.php'))
		exit($erreurKernel . " (0xC02)");
    if(!require_once('../../../'.DIR_INCLUDE . '/pdo.inc.php'))
		exit($erreurKernel . " (0xC04)");
		
	if(isset($_GET['action'])&&isset($_SESSION["id"])){
		switch($_GET['action'])
		{
			case 'update' :
				if(isset($_POST['status'])&&isset($_POST['supplyStatusMsg'])){
					if($_POST['status']=="En ligne" || $_POST['status'] == "Absent" || $_POST['status'] == "Hors ligne"){
						$query = $pdo->prepare("UPDATE forum_membres SET membre_status=?,membre_status_msg=? WHERE membre_id=?");
						if($_POST['status']!="Absent")
							$supplyMsg = '';
						else
							$supplyMsg = $_POST['supplyStatusMsg'];
							
						$query->execute(array($_POST['status'],$supplyMsg,$_SESSION['id']));
					}
				}else{
					echo json_encode(array("Error"=>"Arguments","text"=>"Incomplete Args"));
				}
			break;
			
			case 'get' :
				$query = $pdo->prepare("SELECT membre_status FROM forum_membres WHERE membre_id=?");
				
				$query->execute(array($_SESSION["id"]));
				$fetch = $query->fetch(PDO::FETCH_OBJ);
					echo json_encode(array("status"=>$fetch->membre_status));
			break;
			
			default : 
				echo json_encode(array("Error"=>"Parameter","text"=>"Invalid Command"));	
		}
	}else{
			if(!isset($_GET['action']))
				echo json_encode(array("Error"=>"Parameter","text"=>"No Command Found"));
			if(!isset($_SESSION["id"]))
				echo json_encode(array("Error"=>"Security","text"=>"Violation of security, unauthorized access"));
	}
?>